﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;

namespace TestDxfDocument
{
    class ReplaceableInsertsForDrilling
    {
        public static void ReplaceableInsertWithChipFlute(Dictionary<string, Object> data)  //带容屑槽的可更换刀头
        {
            DxfDocument dxf = new DxfDocument();
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);
            //参数

            double offset = 10;
            double textHeight = 3.5;

            double pi = Math.PI;
            double isframe = Convert.ToDouble(data["isframe"]);
            double sd = Convert.ToDouble(data["dc"]);
            double scale = Convert.ToDouble(data["scale"]);
            if (scale == 0 && sd < 6.3)
            {
                scale = 15.0;
            }
            else if (scale == 0 && sd < 9.5)
            {
                scale = 10.0;
            }
            else if (scale == 0 && sd < 20)
            {
                scale = 5.0;
            }
            else if (scale == 0 && sd < 25)
            {
                scale = 3.5;
            }
            else if (scale == 0 && sd < 35)
            {
                scale = 3;
            }
            else if (scale == 0)
            {
                scale = 2.5;
            }
            double dc = Convert.ToDouble(data["dc"]) * scale; //Convert.ToDouble(data["oal"]);
            double lf = Convert.ToDouble(data["lf"]) * scale; //Convert.ToDouble(data["lpr"]);
            double sig = Convert.ToDouble(data["sig"]) * pi / 180; //Convert.ToDouble(data["lcf"]);
            double pl = Convert.ToDouble(data["pl"]) * scale;
            double lpr = lf + pl;
            string path = Convert.ToString(data["path"]);
            //坐标
            Vector2 v0 = new Vector2(0, 0);
            Vector2 v1 = new Vector2(dc / 2, 0);
            Vector2 v2 = new Vector2(v1.X, -lf);
            Vector2 v3 = new Vector2(v0.X, -lpr);
            Vector2 v4 = new Vector2(-v2.X, v2.Y);
            Vector2 v5 = new Vector2(-v1.X, v1.Y);
            Vector2 v6 = new Vector2(-Math.Tan(sig / 2), v3.Y + 1);
            Vector2 v7 = new Vector2(-v6.X, v6.Y);
            Vector2 v8 = new Vector2(pl * Math.Tan(sig / 2), v2.Y);
            Vector2 v9 = new Vector2(-v8.X, v8.Y);
            //线段
            Vector2[] Line1 = { v1, v2 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { v2, v4 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            Vector2[] Line3 = { v4, v5 };
            fun.drawBLine(dxf, Line3, 0.3, contour);
            Vector2[] Line4 = { v5, v1 };
            fun.drawBLine(dxf, Line4, 0.3, contour);
            Vector2[] Line5 = { v8, v3 };
            fun.drawBLine(dxf, Line5, 0.3, contour);
            Vector2[] Line6 = { v3, v9 };
            fun.drawBLine(dxf, Line6, 0.3, contour);
            Vector2[] Line7 = { new Vector2(0, 2), new Vector2(0, -lpr - 2) };
            fun.drawPlinetype(dxf, Line7, 0.15, center, Linetype.Center);
            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = textHeight;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = textHeight;
            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(v5, v1), offset, 0, dimensionStyle);
            dim1.UserText = "DC=Φ" + Math.Round(dc / scale, 2);
            dxf.AddEntity(dim1);
            //尺寸2
            LinearDimension dim2 = new LinearDimension(new Line(v1, v2), offset, 270, dimensionStyle);
            dim2.UserText = "LF=" + Math.Round(lf / scale, 2);
            dxf.AddEntity(dim2);
            //尺寸3
            LinearDimension dim3 = new LinearDimension(new Line(v2, v3), offset + dc / 4.00, 270, dimensionStyle);
            dim3.UserText = "PL=" + Math.Round(pl / scale, 2);
            dxf.AddEntity(dim3);
            //尺寸4
            Angular2LineDimension dim4 = new Angular2LineDimension(new Line(v3, v6), new Line(v3, v7), offset, dimensionStyle);
            dim4.UserText = String.Format("SIG={0}°", sig / pi * 180);
            dxf.AddEntity(dim4);

            //图框
            if (isframe == 1)
            {
                Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(-148.5, (v1.Y + v3.Y) / 2 + 105),data);
                fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
            }

            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
        public static void ReplaceableInsertFlat(Dictionary<string, Object> data)  //带容屑槽的扁平可换刀头
        {
            DxfDocument dxf = new DxfDocument();
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);
            //参数

            double offset = 10;
            double textHeight = 3.5;


            double pi = Math.PI;
            double isframe = Convert.ToDouble(data["isframe"]); ;
            double sl = Convert.ToDouble(data["lf"]) + Convert.ToDouble(data["dc"]) / 2 / Math.Tan(Convert.ToDouble(data["sig"]) / 2 * pi / 180);
            double scale = Convert.ToDouble(data["scale"]);
            if (scale == 0 && sl < 5)
            {
                scale = 12.6;
            }
            else if (scale == 0 && sl < 10)
            {
                scale = 6.3;
            }
            else if (scale == 0 && sl < 15)
            {
                scale = 4.2;
            }
            else if (scale == 0 && sl < 20)
            {
                scale = 3.15;
            }
            else if (scale == 0 && sl < 25)
            {
                scale = 2.52;
            }
            else if (scale == 0)
            {
                scale = 1;
            }
            double dc = Convert.ToDouble(data["dc"]) * scale; //Convert.ToDouble(data["oal"]);
            double lf = Convert.ToDouble(data["lf"]) * scale; //Convert.ToDouble(data["lpr"]);
            double s = Convert.ToDouble(data["s"]) * scale;
            double sig = Convert.ToDouble(data["sig"]) * pi / 180; //Convert.ToDouble(data["lcf"]);
            double lrgrd = Convert.ToDouble(data["lrgrd"]) * scale;
            double pl = dc / 2 / Math.Tan(sig / 2);
            double lpr = lf + pl;
            string path = Convert.ToString(data["path"]);
            //坐标
            //俯视图
            Vector2 v0 = new Vector2(0, -41.5 + lpr / 2);
            Vector2 v1 = new Vector2(dc / 2, v0.Y);
            Vector2 v2 = new Vector2(v1.X, v1.Y - lf);
            Vector2 v3 = new Vector2(v0.X, v0.Y - lpr);
            Vector2 v4 = new Vector2(-v2.X, v2.Y);
            Vector2 v5 = new Vector2(-v1.X, v1.Y);
            Vector2 v6 = new Vector2(-Math.Tan(sig / 2), v3.Y + 1);
            Vector2 v7 = new Vector2(-v6.X, v6.Y);
            Vector2 v8 = new Vector2(-Math.Sqrt(dc * dc / 4 - s * s / 4), v0.Y);
            Vector2 v9 = new Vector2(v8.X, v4.Y);
            Vector2 v10 = new Vector2(-v9.X, v9.Y);
            Vector2 v11 = new Vector2(-v8.X, v8.Y);
            Vector2 v12 = new Vector2(0, v3.Y + s / 2 / Math.Tan(sig / 2));
            //主视图
            Vector2 v13 = new Vector2(0, 41.5);
            Vector2 v14 = new Vector2(v8.X, v13.Y + s / 2);
            Vector2 v15 = new Vector2(-v14.X, v14.Y);
            Vector2 v16 = new Vector2(v15.X, v15.Y - s);
            Vector2 v17 = new Vector2(-v16.X, v16.Y);
            Vector2 v18 = new Vector2(0, lf - lrgrd + v3.Y);
            Vector2 v19 = new Vector2(v1.X + 2, v1.X / Math.Tan(sig / 2) + v18.Y + 2 / Math.Tan(sig / 2));
            Vector2 v20 = new Vector2(-v19.X, v19.Y);
            Vector2 v21 = new Vector2(v1.X, v1.X / Math.Tan(sig / 2) + v18.Y);
            //线段
            //俯视图
            Vector2[] Line1 = { v1, v2 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { v2, v3 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            Vector2[] Line3 = { v3, v4 };
            fun.drawBLine(dxf, Line3, 0.3, contour);
            Vector2[] Line4 = { v4, v5 };
            fun.drawBLine(dxf, Line4, 0.3, contour);
            Vector2[] Line5 = { v5, v1 };
            fun.drawBLine(dxf, Line5, 0.3, contour);
            Vector2[] Line6 = { v8, v9 };
            fun.drawBLine(dxf, Line6, 0.3, contour);
            Vector2[] Line7 = { v10, v11 };
            fun.drawBLine(dxf, Line7, 0.3, contour);
            Vector2[] Line8 = { v4, v9 };
            fun.drawBLine(dxf, Line8, 0.3, contour);
            Vector2[] Line9 = { v10, v2 };
            fun.drawBLine(dxf, Line9, 0.3, contour);
            Vector2[] Line10 = { new Vector2(0, v0.Y + 2), new Vector2(0, v3.Y - 2) };
            fun.drawPlinetype(dxf, Line10, 0.15, center, Linetype.Center);
            Vector2[] Line15 = { v18, v19 };
            fun.drawPlinetype(dxf, Line15, 0.15, refin, Linetype.Dashed);
            Vector2[] Line16 = { v18, v20 };
            fun.drawPlinetype(dxf, Line16, 0.15, refin, Linetype.Dashed);
            LwPolyline Arc1 = new LwPolyline();
            Arc1.Vertexes.Add(new LwPolylineVertex(v10));
            Arc1.Vertexes.Add(new LwPolylineVertex(v9));
            Arc1.Vertexes[0].Bulge = -(v9.Y - v12.Y) / 2 / v10.X;
            fun.PolyFill(dxf, Arc1, 0.3, contour);
            //主视图
            Vector2[] Line11 = { v14, v15 };
            fun.drawBLine(dxf, Line11, 0.3, contour);
            Vector2[] Line12 = { v16, v17 };
            fun.drawBLine(dxf, Line12, 0.3, contour);
            Vector2[] Line13 = { new Vector2(0, v14.Y + 2), new Vector2(0, v17.Y - 2) };
            fun.drawPlinetype(dxf, Line13, 0.15, center, Linetype.Center);
            Vector2[] Line14 = { new Vector2(-dc / 2 - 2, v13.Y), new Vector2(dc / 2 + 2, v13.Y) };
            fun.drawPlinetype(dxf, Line14, 0.15, center, Linetype.Center);
            LwPolyline Arc2 = new LwPolyline();
            Arc2.Vertexes.Add(new LwPolylineVertex(v17));
            Arc2.Vertexes.Add(new LwPolylineVertex(v14));
            Arc2.Vertexes[0].Bulge = -(dc / 2 - v15.X) / (s / 2);
            fun.PolyFill(dxf, Arc2, 0.3, contour);
            LwPolyline Arc3 = new LwPolyline();
            Arc3.Vertexes.Add(new LwPolylineVertex(v15));
            Arc3.Vertexes.Add(new LwPolylineVertex(v16));
            Arc3.Vertexes[0].Bulge = -(dc / 2 - v15.X) / (s / 2);
            fun.PolyFill(dxf, Arc3, 0.3, contour);
            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = textHeight;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = textHeight;
            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(v5, v1), offset, 0, dimensionStyle);
            dim1.UserText = "DC=Φ" + Math.Round(dc / scale, 2);
            dxf.AddEntity(dim1);
            //尺寸2
            LinearDimension dim2 = new LinearDimension(new Line(v1, v2), offset * 2, 270, dimensionStyle);
            dim2.UserText = "LF=" + Math.Round(lf / scale, 2);
            dxf.AddEntity(dim2);
            //尺寸3
            LinearDimension dim3 = new LinearDimension(new Line(v2, v3), offset * 2 + dc / 4.00, 270, dimensionStyle);
            dim3.UserText = "PL=" + Math.Round(pl / scale, 2);
            dxf.AddEntity(dim3);
            //尺寸4
            Angular2LineDimension dim4 = new Angular2LineDimension(new Line(v3, v6), new Line(v3, v7), offset*2, dimensionStyle);
            dim4.UserText = String.Format("SIG={0}°", sig / pi * 180);
            dxf.AddEntity(dim4);
            //尺寸5
            LinearDimension dim5 = new LinearDimension(new Line(v1, v3), offset * 3 + dc / 4.00, 270, dimensionStyle);
            dim5.UserText = "OAL.LPR=" + Math.Round(lpr / scale, 2);
            dxf.AddEntity(dim5);
            //尺寸6
            LinearDimension dim6 = new LinearDimension(new Line(v15, v16), offset + dc / 2 - v15.X, 270, dimensionStyle);
            dim6.UserText = "S=" + Math.Round(s / scale, 2);
            dxf.AddEntity(dim6);
            //尺寸7
            LinearDimension dim7 = new LinearDimension(new Line(v1, v21), offset, 270, dimensionStyle);
            dim7.UserText = "LRGRD=" + Math.Round(lrgrd / scale, 2);
            dxf.AddEntity(dim7);
            //图框
            if (isframe == 1)
            {
                Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(-105, 148.5),data);
                fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
            }
            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
